﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using _2QWebProxy.App_Code;

namespace _2QWebProxy.GUI
{
    public partial class ucWebsiteDenyFileType : UserControl
    {
        #region Utils Methods

        private void LoadWebsites()
        {
            List<PrivacyDomain> list = Utils.GetPrivacyDomain("allow");
            cbbWebsite.Items.Add("Add new website");
            if (list.Count > 0)
            {
                foreach (PrivacyDomain domain in list)
                {
                    cbbWebsite.Items.Add(domain);
                }
                cbbWebsite.DisplayMember = "Name";
            }
            cbbWebsite.SelectedIndex = 0;
        }

        private void LoadFielTypes()
        {
            List<string> list = Utils.GetCommonFileType();
            cbbFileType.DataSource = list;
        }

        private void CheckControlDomain()
        {
            txtNewWebsite.Visible = cbbWebsite.SelectedIndex == 0;
        }

        private void LoadListData()
        {
            dgvData.Rows.Clear();
            List<DenyFileType> list = Utils.GetListWebsiteDenyFileType();
            bool flag = false;
            int num = 0;
            for (int i = 0; i < list.Count; i++)
            {
                flag = true;
                foreach (string file in list[i].FileTypes)
                {
                    dgvData.Rows.Add();
                    if (flag)
                        dgvData.Rows[num].Cells["WebsiteName"].Value = list[i].Name;
                    dgvData.Rows[num].Cells["FileType"].Value = file;
                    dgvData.Rows[num].Cells["WebsiteNameHidden"].Value = list[i].Name;
                    num++;
                    flag = false;
                }
            }
        }

        #endregion

        public ucWebsiteDenyFileType()
        {
            InitializeComponent();
            LoadWebsites();
            LoadFielTypes();
            CheckControlDomain();
            LoadListData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (cbbWebsite.SelectedIndex == 0)
            {
                Utils.SaveWebsiteDenyFileType(txtNewWebsite.Text, cbbFileType.Text);
            }
            else
                Utils.SaveWebsiteDenyFileType(cbbWebsite.Text, cbbFileType.Text);
            LoadListData();
        }

        private void cbbWebsite_SelectedIndexChanged(object sender, EventArgs e)
        {
            CheckControlDomain();
        }

        private void dgvData_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = dgvData.SelectedCells[0].RowIndex;
            cbbWebsite.Text = dgvData.Rows[index].Cells["WebsiteNameHidden"].Value.ToString();
            cbbFileType.Text = dgvData.Rows[index].Cells["FileType"].Value.ToString();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            Utils.DeleteWebsiteDenyFileType(cbbWebsite.Text, cbbFileType.Text);
            LoadListData();
        }

        private void btnRemoveAll_Click(object sender, EventArgs e)
        {
            Utils.DeleteAllWebsiteDenyFileType(cbbWebsite.Text, cbbFileType.Text);
            LoadListData();
        }



    }
}
